<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
// import { createTableDataApi, deleteTableDataApi, updateTableDataApi, getTableDataApi } from "@/api/table"
import { getOutWarehouseOrderDetApi } from "@/api/rewrite"
// import { type CreateOrUpdateTableRequestData, type GetTableData } from "@/api/table/types/table"
// import { type GetTableResponseData } from "@/api/log/types/log"
import { type FormInstance } from "element-plus"
import { Search, Refresh, Delete, Download, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { useRouter } from "vue-router"

const router = useRouter()

defineOptions({
  // 命名当前组件
  name: "logList"
})

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const pageData = ref({
  dialogErrorMessageVisible: false,
  currentShowErrorMessage: ""
})

//#region 查
const tableData = ref<any>([])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  // orderName: "",
})
const getTableData = () => {
  loading.value = true
  getOutWarehouseOrderDetApi({
    startPage: paginationData.startPage,
    pageSize: paginationData.pageSize,
    ...searchData
  })
    .then((res) => {
      paginationData.total = res.count
      tableData.value = res.data
      console.log(tableData.value)
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  paginationData.startPage === 1 ? getTableData() : (paginationData.startPage = 1)
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}

const rowDbclick = (row: any) => {
  console.log(row)
  // console.log(events)

  router.push(`log-detail/${row.logId}`)
}

//#endregion

/** 监听分页参数的变化 */
watch([() => paginationData.startPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <!-- <el-form-item prop="username" label="用户名">
          <el-input v-model="searchData.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="phone" label="手机号">
          <el-input v-model="searchData.phone" placeholder="请输入" />
        </el-form-item> -->
        <!-- <el-form-item prop="orderName" label="接口名称">
          <el-input v-model="searchData.orderName" placeholder="请输入" />
        </el-form-item> -->

        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <!-- <el-button type="primary" :icon="CirclePlus" @click="dialogVisible = true">新增用户</el-button> -->
          <el-button type="danger" :icon="Delete">批量删除</el-button>
        </div>
        <div>
          <el-tooltip content="下载">
            <el-button type="primary" :icon="Download" circle />
          </el-tooltip>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" @row-dblclick="rowDbclick">
          <el-table-column type="selection" width="50" align="center" />
          <!-- <el-table-column prop="username" label="用户名" align="center" />
          <el-table-column prop="roles" label="角色" align="center">
            <template #default="scope">
              <el-tag v-if="scope.row.roles === 'admin'" effect="plain">admin</el-tag>
              <el-tag v-else type="warning" effect="plain">{{ scope.row.roles }}</el-tag>
            </template>
          </el-table-column> -->
          <el-table-column prop="outWarehouseOrderCode" width="200" label="出库单号" align="center" />
          <el-table-column prop="outWarehouseOrderDetId" width="200" label="出库单明细id" align="center" />

          <el-table-column prop="reStatus1" width="200" label="311反写结果" align="center">
            <template #default="scope">
              <el-tag v-if="scope.row.reStatus1 == 0">未反写</el-tag>
              <el-tag v-if="scope.row.reStatus1 == 2" type="success">成功</el-tag>
              <el-tag v-if="scope.row.reStatus1 == 3" type="warning">失败</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="reCode1" width="200" label="311凭证号" align="center" />
          <el-table-column prop="re1StoreCode" width="200" label="311库存地点" align="center" />
          <el-table-column prop="reErrorMsg1" label="311异常信息" align="center" />
          <el-table-column prop="modifiedTime" width="300" label="明细修改时间" align="center" />
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.startPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新增/修改 -->
    <!-- <el-dialog
      v-model="dialogVisible"
      :title="formData.id === undefined ? '新增用户' : '修改用户'"
      @closed="resetForm"
      width="30%"
    >
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" label-position="left">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="formData.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="密码" v-if="formData.id === undefined">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateOrUpdate" :loading="loading">确认</el-button>
      </template>
    </el-dialog> -->

    <el-dialog v-model="pageData.dialogErrorMessageVisible" title="异常信息" width="70%">
      <div class="error-log">{{ pageData.currentShowErrorMessage }}</div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}

.keywordDiv {
  // max-width: 200px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}

.errorMsgdiv {
  // max-width: 200px;
  color: red;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}

.error-log {
  background-color: #333; /* 深灰背景 */
  color: #bada55; /* 亮绿色文字 */
  font-family: "Courier New", monospace; /* 等宽字体 */
  padding: 10px; /* 内边距 */
  border-radius: 5px; /* 边角圆滑 */
  border: 1px solid #666; /* 边框颜色稍浅 */
  white-space: pre-wrap; /* 保持换行和空格 */
  word-wrap: break-word; /* 长单词也可以断行 */
  // max-width: 600px; /* 最大宽度 */
  margin: 20px auto; /* 居中显示 */
}
</style>
